<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
  <meta name="generator" content="Hugo 0.88.1">
  <title>欢迎来到您的发布页面</title>

  <link rel="canonical" href="https://getbootstrap.com/docs/4.6/examples/dashboard/">



  <!-- Bootstrap core CSS -->
  <link href="./bootstrap.min.css" rel="stylesheet">





  <style>
    .bd-placeholder-img {
      font-size: 1.125rem;
      text-anchor: middle;
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }

    @media (min-width: 768px) {
      .bd-placeholder-img-lg {
        font-size: 3.5rem;
      }
    }
    thead th{
      text-align: center;
     
    }

    tbody td {
     text-align: center;
     line-height: 100px;
    }
    tbody td:nth-last-child(1){
     line-height: 20px;
     padding-top: 40px;
    }
    tbody img{
      width: 100px;
    }
    input{
      outline: none;
    }
  </style>


  <!-- Custom styles for this template -->
  <link href="dashboard.css" rel="stylesheet">
</head>

<body>

  <nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
    <a class="navbar-brand col-md-3 col-lg-2 mr-0 px-3" href="#">良仓</a>
    <button class="navbar-toggler position-absolute d-md-none collapsed" type="button" data-toggle="collapse"
      data-target="#sidebarMenu" aria-controls="sidebarMenu" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <input class="form-control form-control-dark w-100" type="text" placeholder="搜索" aria-label="Search">
    <ul class="navbar-nav px-3">
      <li class="nav-item text-nowrap">
        <a class="nav-link" href="../index.html">退出</a>
      </li>
    </ul>
  </nav>

  <div class="container-fluid">
    <div class="row">
      <nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
        <div class="sidebar-sticky pt-3">
          <ul class="nav flex-column">
            <li class="nav-item">
              <a class="nav-link active" href="#">
                <span data-feather="home"></span>
                商品 <span class="sr-only">(current)</span>
              </a>
            </li>
          </ul>

         
        </div>
      </nav>

      <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-md-4">
        <div
          class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
          <h1 class="h2">商品发布</h1>
          <div class="btn-toolbar mb-2 mb-md-0">
            <button type="button" class="btn btn-sm btn-outline-secondary dropdown-toggle">
              <span data-feather="calendar"></span>
              本周
            </button>
          </div>
          <input type="text" name="" id="pimg" placeholder="图片地址"/>
          <input type="text" name="" id="pname" placeholder="标题"/>
          <input type="text" name="" id="pprice" placeholder="价格"/>
          <input type="text" name="" id="pdesc" placeholder="详情"/>
          <input type="button" name="" id="" value="发布" class=" btn btn-primary" />


        </div>

        <!-- <canvas class="my-4 w-100" id="myChart" width="900" height="380"></canvas> -->

        <h2>商品信息</h2>
        <div class="table-responsive">
          <table class="table table-striped table-sm">
            <thead>
              <tr>
                <th>id</th>
                <th>标题</th>
                <th>图片</th>
                <th>价格</th>
                <th>详情</th>
                <th>操作</th>

              </tr>
            </thead>
            <tbody>
              <!-- <tr>
              <td>1,001</td>
              <td>random</td>
              <td>data</td>
              <td>placeholder</td>
              <td>text</td>
            </tr> -->

            </tbody>
          </table>
        </div>
      </main>
    </div>
  </div>


  <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"
    integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
    crossorigin="anonymous"></script>
  <script>window.jQuery || document.write('<script src="../assets/js/vendor/jquery.slim.min.js"><\/script>')</script>
  <script src="./bootstrap.bundle.min.js"></script>


  <script src="https://cdn.jsdelivr.net/npm/feather-icons@4.28.0/dist/feather.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.min.js"></script>
  <script src="dashboard.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


  <script>
    /*   所有商品接口
          get
          参数
          pagesize  每页获取几个数据
          pagenum 获取第几页数据
          uid  用户id
          http://jx.xuzhixiang.top/ap/api/allproductlist.php?pagesize=20&pagenum=4 */

    let pagesize = 50;
    let pagenum = 0;
    // 用户id
    let id = localStorage.getItem("id");
    let token = localStorage.getItem("token");
    laodList();
    // 封装加载商品列表的函数 --- 页面一开始需要 加载商品列表-- 发布了商品列表得更新 加载商品列表
    function laodList() {
      let listAPI = "http://jx.xuzhixiang.top/ap/api/allproductlist.php";

      $.get(listAPI, { pagesize, pagenum, uid: id, }).then(r => {
        console.log(r);
        let productArr = r.data;
        // 根据商品数组- 拼接字符串 给ul
        let str = "";
        productArr.forEach((pObj) => {
          str += `

            <tr>
              <td>${pObj.pid}</td>
              <td>${pObj.pname}</td>
              <td> <img src="${pObj.pimg}" alt=""></td>
              <td>${pObj.pprice}</td>
              <td>${pObj.pdesc}</td>
              <td> 
                <button onclick="delProduct(${pObj.pid})" class="btn-primary">删除</button>
                <a href="update.html?id=${pObj.pid}">修改</a>
              </td>
            </tr>
                `;
        });
        $("tbody").html(str);
      });

    }


    /*
 添加商品 接口

接口地址：  http://jx.xuzhixiang.top/ap/api/goods/goods-add.php
  接口请求方式：get
  接口参数：
 1.
pimg 2.
pname 3.
pprice 4.
pdesc 5.
uid （可选）用户商品

  使用方式  post 请求，参数在请求体

 */

    /*  */

    $(".btn").click(function () {
      let pimg = $("#pimg").val();
      let pname = $("#pname").val();
      let pprice = $("#pprice").val();
      let pdesc = $("#pdesc").val();
      let addProductAPI =
        "http://jx.xuzhixiang.top/ap/api/goods/goods-add.php";
      $.get(addProductAPI, { pimg, pname, pprice, pdesc, uid: id }).then(r => {
        console.log(r);
        laodList();
      })
    })
    // 删除函数

    /* 
    接口地址：    http://jx.xuzhixiang.top/ap/api/goods/goods-delete.php
       接口请求方式：get
       接口参数：
 
              pid（必选）商品的id
              uid（必选）用户的id
               token（必选）登陆时候返回的token
 
                使用方式
               http://jx.xuzhixiang.top/ap/api/goods/goods-delete.php?pid=2459
 
    */
    async function delProduct(pid) {
      console.log(pid);
      // 准备删除接口参数
      let params = { pid, uid: id, token };
      console.log(params);
      let productDelteAPI =
        "http://jx.xuzhixiang.top/ap/api/goods/goods-delete.php";
      let res = await $.get(productDelteAPI, params);
      console.log(res);
      //调用 加载列表
      laodList();
    }
  </script>
</body>

</html>